/*
* Copyright (c) 2012 Kyle Hayward
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "Precompiled.h"

#include "Geometry.h"
#include "Context.h"
#include "Variable.h"
#include "Program.h"

using namespace System;

namespace OptixDotNet 
{
	Geometry::Geometry( Context^ context ) : OptixNode( context )
	{
		mGeo = 0;

		pin_ptr<RTgeometry> ptr = &mGeo;
		CheckError( rtGeometryCreate( context->InternalPtr(), ptr ) );
	}

	Geometry::Geometry( Context^ context, RTgeometry geom ) : OptixNode( context )
	{
		mGeo = geom;
	}

	Program^ Geometry::IntersectionProgram::get()
	{
		RTprogram program;
		CheckError( rtGeometryGetIntersectionProgram( mGeo, &program ) );

		return gcnew Program( mContext, program );
	}

	void Geometry::IntersectionProgram::set( Program^ program )
	{
		CheckError( rtGeometrySetIntersectionProgram( mGeo, program->InternalPtr() ) );
	}

	Program^ Geometry::BoundingBoxProgram::get()
	{
		RTprogram program;
		CheckError( rtGeometryGetBoundingBoxProgram( mGeo, &program ) );

		return gcnew Program( mContext, program );
	}

	void Geometry::BoundingBoxProgram::set( Program^ program )
	{
		CheckError( rtGeometrySetBoundingBoxProgram( mGeo, program->InternalPtr() ) );
	}

	Variable^ Geometry::default::get( String^ s )
	{
		const char* str = (const char*)StrToAnsii( s );
		
		RTvariable rtVar;
		CheckError( rtGeometryQueryVariable( mGeo, str, &rtVar ) );

		if( rtVar == 0 ) 
			CheckError( rtGeometryDeclareVariable( mGeo, str, &rtVar ) );

		return gcnew Variable( mContext, rtVar );
	}

	void Geometry::default::set( String^ s, Variable^ var )
	{
		if( String::IsNullOrEmpty( s ) )
			throw gcnew Exception( "Geometry Error: Variable name is null or empty" );

		const char* str = (const char*)StrToAnsii( s );

		RTvariable rtVar;
		CheckError( rtGeometryQueryVariable( mGeo, str, &rtVar ) );

		if( rtVar != 0 && var == nullptr )
		{
			CheckError( rtGeometryRemoveVariable( mGeo, rtVar ) );
		}
		else
		{
			if( String::IsNullOrEmpty( s ) )
				throw gcnew Exception( "Geometry Error: Variable copying not yet implemented" );
		}
	}

	Variable^ Geometry::default::get( int index )
	{
		if( index < 0 || index >= VariableCount )
			throw gcnew ArgumentOutOfRangeException( "index" );
		
		RTvariable rtVar;
		CheckError( rtGeometryGetVariable( mGeo, index, &rtVar ) );

		return gcnew Variable( mContext, rtVar );
	}

	void Geometry::default::set( int index, Variable^ var )
	{
		if( index < 0 || index >= VariableCount )
			throw gcnew ArgumentOutOfRangeException( "index" );

		RTvariable rtVar;
		CheckError( rtGeometryGetVariable( mGeo, index, &rtVar ) );

		if( var == nullptr )
		{
			CheckError( rtGeometryRemoveVariable( mGeo, rtVar ) );
		}
		else
		{
			throw gcnew Exception( "Geometry Error: Variable copying not yet implemented" );
		}
	}
}